问题
I have a simple shiny app in which I use a numericInput()
to set the number of the rows of the dataframe.Then I use a selectInput()
"Label" to select a Label from the datatable and then change its name with textInput() "Change to". The problem is that every time I try to change the name of a new Label using the textInput()
the previous Label that I modified returns to default name. I believe this is happening because my DF
is created inside a reactive function and cannot accept the subsetting. What I probably need is using reactiveValues()
in order to store the previous values but I do not know exactly how to use it so I use reactive()
here in order to display a working example.
library(shiny)
library(DT)
ui <- navbarPage(
"Application",
tabPanel("General",
sidebarLayout(
sidebarPanel(
uiOutput("tex2"),
uiOutput("book3"),
uiOutput("book6")
),
mainPanel(
DT::dataTableOutput("hot3")
)
)))
#server.r
server <- function(input, output,session) {
output$tex2<-renderUI({
numericInput("text2", "#tests", value = 1, min=1)
})
output$book3<-renderUI({
selectInput("bk3", "Label", choices=(paste("Test",1:input$text2)))
})
output$book6<-renderUI({
textInput("bk6", "Change to", value=NULL)
})
rt4<-reactive({
DF <- data.frame(
Test=paste(1:input$text2),
Label=paste("Test",1:input$text2),
stringsAsFactors = FALSE)
if(!is.null(input$bk6) && input$bk6!=""){
DF[DF$Label==isolate(input$bk3), "Label"] <- input$bk6
}
{
DF
}
})
output$hot3 <-DT::renderDataTable(
rt4(),
rownames= FALSE
)
}
回答1:
This seems to work
library(shiny)
library(DT)
library(dplyr)
ui <- navbarPage(
"Application",
tabPanel("General",
sidebarLayout(
sidebarPanel(
uiOutput("tex2"),
uiOutput("book3"),
uiOutput("book6"),
actionButton('submit','submit')
),
mainPanel(
DT::dataTableOutput("hot3")
)
)))
server <- function(input, output,session) {
output$tex2<-renderUI({
numericInput("text2", "#tests", value = 1, min=1)
})
output$book3<-renderUI({
selectInput("bk3", "Label", choices=(paste("Test",1:input$text2)) )
})
output$book6<-renderUI({
textInput("bk6", "Change to", value=NULL)
})
values <- reactiveValues(rv = NULL)
observe( { req(input$text2)
Test <-paste(1: input$text2)
Label <- paste("Test",1:input$text2)
values$rv<-data.frame( Test= Test, Label=Label, stringsAsFactors = FALSE)}
)
observeEvent(input$submit,{
if(!is.null(input$bk6) && input$bk6!=""){
values$rv <- values$rv %>%
mutate(Label= ifelse(Label== input$bk3,input$bk6,Label))
}
})
output$hot3 <-DT::renderDataTable( {
datatable(values$rv, rownames=FALSE)
})
}
shinyApp(ui,server)
回答2:
If you store your dataframe in reactiveValues
object, you can update the data according to the inputs e.g. like that:
library(shiny)
library(DT)
ui <- navbarPage(
"Application",
tabPanel("General",
sidebarLayout(
sidebarPanel(
uiOutput("tex2"),
uiOutput("book3"),
uiOutput("book6")
),
mainPanel(
DT::dataTableOutput("hot3")
)
)))
#server.r
server <- function(input, output,session) {
output$tex2<-renderUI({
numericInput("text2", "#tests", value = 1, min=1)
})
# update selectinput reactiveValues change
output$book3<-renderUI({
selectInput("bk3", "Label", choices=rtdf4$DF[, 'Label'])
})
output$book6<-renderUI({
textInput("bk6", "Change to", value=NULL)
})
rtdf4 <- reactiveValues()
# fill reactiveValues based on the numberinput
observe({
if (is.null(input$text2)){
rtdf4$DF <- data.frame(
Test=paste(1),
Label=paste("Test",1),
stringsAsFactors = FALSE
)
} else {
rtdf4$DF <- data.frame(
Test=paste(1:input$text2),
Label=paste("Test",1:input$text2),
stringsAsFactors = FALSE
)
}
})
rt4 <- reactive({
if (is.null(rtdf4$DF))
return(NULL)
if(!is.null(input$bk6) && input$bk6!=""){
rtdf4$DF[rtdf4$DF$Label==isolate(input$bk3), "Label"] <- input$bk6
}
{
rtdf4$DF
}
})
output$hot3 <-DT::renderDataTable(
rt4(),
rownames= FALSE
)
}
shinyApp(ui,server)
来源:https://stackoverflow.com/questions/51981821/how-to-use-reactivevalues-in-order-to-store-changes-in-a-shiny-app