I have started writing a shiny app.
What I want to achieve is loading a text file with a string in one line - like this:
$ cat testdata.txt
hello world
If the text gets loaded, the vowels are set to value 1 in the data frame and are highlighted with the shinyBS
package.
What I want to achieve is to be able to change the values in the data frame by clicking the toggled buttons. The buttons can be toggled, but the changed values are not transferred to the data frame or the plot.
How can this be achieved? I hope the question is clear, if not please ask and I will try to rephrase the question and make it clearer.
Here is the code for ui.R
and server.R
.
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Test buttons"),
sidebarPanel(
fileInput('file1', 'Choose text file',
accept=c('text',
'text/plain',
'.txt')),
tags$hr()
),
mainPanel(
tabsetPanel(
tabPanel("Home"),
tabPanel("Loaded Text",
br(),
tableOutput("showOverview")),
tabPanel("Text fields",
br(),
uiOutput("createButtons")),
tabPanel("Plots",
br(),
plotOutput("showDistribution"))
)
)
))
server.R
library(shiny)
library(shinyBS)
shinyServer(function(input, output) {
fileReadText <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
uploadReadText <- read.table(inFile$datapath, sep = "|",
stringsAsFactors = FALSE)
df <- data.frame(V1 = unlist(strsplit(uploadReadText[1,], "")))
df$V2 <- as.integer(grepl("[aeiou]", df$V1))
df$V2 <- ifelse(df$V1 == " ", NA, df$V2)
names(df) <- c("letter", "value")
df
})
output$showOverview <- renderTable({
data1 <- fileReadText()
data1[c(1:10),]
})
output$createButtons <- renderUI({
data1 <- fileReadText()
listOfButtons = list()
for (i in 1:length(data1$letter)) {
buttonValue <- as.logical(data1$value[i])
buttonDisabled = FALSE
if (is.na(buttonValue)) {
buttonValue = 0
buttonDisabled = TRUE
}
listOfButtons <- list(listOfButtons,
bsButton(paste("button_", i, sep = ""),
data1$letter[i],
type = "toggle",
value = as.logical(buttonValue),
disabled = buttonDisabled))
}
listOfButtons
})
output$showDistribution <- renderPlot({
data1 <- fileReadText()
plot(data1$value)
})
})