How to stop recreating a dataset at each rendering of a shiny app

How to stop recreating a dataset at each rendering of a shiny app



I have a simple shiny app in which 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 I want the application to keep all the name changes I make and not returning to the default name when I choose another Label. I think the problem is that the DF exists only within rt4 function and not outside it so it's recreated at each rendering.


#ui.r
navbarPage(
"Application",
tabPanel("General",
sidebarLayout(

sidebarPanel(
uiOutput("book3"),
uiOutput("book6")
),
mainPanel(

DT::dataTableOutput("hot3")

)
)))
#server.r
library(shiny)
library(DT)
server <- function(input, output,session)



output$book3<-renderUI(
selectInput("bk3",
"Label",
choices=(paste("Test",1:5)))
)

output$book6<-renderUI(
textInput("bk6", "Change to",
value=NULL
)
)

rt4<-reactive(input$bk6=="")
DF=data.frame(
Test=paste(1:5),
Label=paste("Test",1:5),
stringsAsFactors = FALSE)

else
DF[DF==input$bk3]<-input$bk6

DF

)

output$hot3 <-DT::renderDataTable(
rt4(),
rownames= FALSE

)





1 Answer
1



Important changes:

- as you expected, DF is defined once, in the server, see shiny scoping rules

- use <<- to modify the global (server) DF

- use isolate() to react only to the change of input$bk6


DF


<<-


DF


isolate()


input$bk6


library(shiny)
library(DT)

ui <- navbarPage(
"Application",
tabPanel("General",
sidebarLayout(
sidebarPanel(
uiOutput("book3"),
uiOutput("book6")
),
mainPanel(
DT::dataTableOutput("hot3")
)
)))

server <- function(input, output,session)

DF <- data.frame(
Test=paste(1:5),
Label=paste("Test",1:5),
stringsAsFactors = FALSE)

output$book3<-renderUI(
selectInput("bk3", "Label", choices=(paste("Test",1:5)))
)

output$book6<-renderUI(
textInput("bk6", "Change to", value=NULL)
)

rt4<-reactive(
if(!is.null(input$bk6) && input$bk6!="")
DF[DF$Label==isolate(input$bk3), "Test"] <<- input$bk6

DF
)

output$hot3 <-DT::renderDataTable(
rt4(),
rownames= FALSE

)


shinyApp(ui, server)





I accepted your answer as it is correct for the original post but I also created a similar question here stackoverflow.com/questions/51967497/…
– firmo23
Aug 22 at 13:01






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ