Hello the following code returns an error and the app crashes. The error refers to "You tried to do something that can only be done from inside a reactive expression or observer".
library(shiny)
ui <- fluidPage(
theme = "",
titlePanel(""),
sidebarPanel(textInput("freetext", "Write Something")),
mainPanel(textOutput("testtext"))
)
server <- function(input, output) {
output$testtext <- renderText(paste("I wrote: ", input$freetext))
table <- data.frame(wrote = input$freetext)
write.csv(table, "~/testtable.csv", row.names = FALSE)
}
shinyApp(ui, server)
I have tried to add the input$freetext into a reactive element but with no luck.
server <- function(input, output) {
output$testtext <- renderText(paste("I wrote: ", input$freetext))
outtext <- reactive({input$freetext})
table <- data.frame(wrote = outtext())
write.csv(table, "~/testtable.csv", row.names = FALSE)
}
How can I add the value from input$freetext to the data.frame?
Answer: This worked
library(shiny)
ui <- fluidPage(
theme = "",
titlePanel(""),
sidebarPanel(textInput("freetext", "Write Something")),
mainPanel(textOutput("testtext"))
)
server <- function(input, output) {
output$testtext <- renderText(paste("I wrote: ", input$freetext))
observeEvent(input$freetext, {
if(nchar(input$freetext) > 0) {
t <-input$freetext
table <- data.frame(wrote = t)
write.csv(table, "/shiny-data/users/scotcan1/testApp/testtable.csv", row.names = FALSE)
}
})
}
shinyApp(ui, server)
question from:
https://stackoverflow.com/questions/65940372/who-to-write-a-table-using-a-shiny-input 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…